/**
 * Created on [2020/11/29 09:21] by zhengrr
 * <p>
 * 项目名称:
 * <p>
 * 本程序版权属于福州慧政通信息科技有限公司所有。
 * <p>
 * 任何组织和个人未经福州迪创信息科技有限公司许可与授权,不得擅自传播、复制、更改该程序的内容。
 * 本程序受版权法和国际条约的保护。如未经授权而擅自复制或传播本程序(或其中任何部分),
 * 将受到严厉的刑事及民事制裁，并将在法律许可的范围内受到最大可能的起诉!
 * <p>
 * ©2020 福州慧政通信息科技有限公司
 */
package com.sunnada.btfvilg.controller;

import com.sunnada.btfvilg.common.response.Result;
import com.sunnada.btfvilg.common.response.ResultFactory;
import com.sunnada.btfvilg.entity.RemoteLayer;
import com.sunnada.btfvilg.model.remotelayer.FileVo;
import com.sunnada.btfvilg.service.RemoteLayerService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传相关的接口
 *
 * @author huangcx
 */
@RestController
@RequestMapping("remotelayer")
@Api(tags = "文件")
public class RemoteLayerController {

    @Autowired
    private RemoteLayerService remoteLayerService;

    @PostMapping(value = "/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "上传文件", httpMethod = "POST")
    @ApiImplicitParams({
    })
    public Result<Integer> createFile(@ApiParam(value = "上传的文件", required = true) MultipartFile file) {
        try {
            return ResultFactory.success(remoteLayerService.uploadFile(file));
        } catch (Exception e) {
            return ResultFactory.failed(e.getMessage(), 0);
        }
    }

    @GetMapping("/info")
    @ApiOperation(value = "获取服务器已有文件信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileName", value = "文件名", dataType = "String", paramType = "query", required = true)
    })
    public Result<List<FileVo>> getFilesByName(String fileName) throws Exception {
        if (fileName == null) {
            return ResultFactory.failed("文件名不能为空。",null);
        }

        return ResultFactory.success(remoteLayerService.getFilesByName(fileName));
    }

    @GetMapping("/getInfoByIds")
    @ApiOperation(value = "通过id获取服务器已有文件信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "id列表，逗号分隔", dataType = "String", paramType = "query", required = true)
    })
    public Result<List<RemoteLayer>> getFilesByIds(String ids) throws Exception {
        if (ids == null) {
            return ResultFactory.failed("id列表不能为空。",null);
        }

        String[] split = ids.split(",");
        ArrayList<Integer> infoid = new ArrayList<>(split.length);
        for (String id : split)
            infoid.add(Integer.valueOf(id));

        return ResultFactory.success(remoteLayerService.getFilesByIds(infoid));
    }

    @PostMapping("/saveInfo")
    @ApiOperation(value = "保存按钮，保存新名字及状态", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query", required = true)
    })
    public Result<Integer> saveNameAndState(Integer id,String name) throws Exception {
        if (id == null) {
            return ResultFactory.failed("id不能为空。",null);
        }

        if (name == null) {
            return ResultFactory.failed("名称不能为空。",null);
        }

        return ResultFactory.success(remoteLayerService.saveNameAndState(id,name));
    }

    @GetMapping("/downloadFile")
    @ApiOperation(value = "下载文件", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文件ID", dataType = "Integer", paramType = "Path", required = true)
    })
    public void downloadFile(Integer id, HttpServletResponse response) {
        remoteLayerService.downloadFile(id, response);
    }

    @DeleteMapping("/deleteInfo")
    @ApiOperation(value = "删除指定ID数据信息", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", dataType = "String", paramType = "query", required = true)
    })
    public Result<Boolean> deleteInfo(Long id) throws Exception {
        return ResultFactory.success(remoteLayerService.deleteInfo(id));
    }

}
